<?php

App::uses('CrudController', 'Controller');

class TeamsController extends CrudController
{
    public $_model = 'Team';

    public function index() {
        $this->request->allowMethod('get');
        $this->loadModel('Team');
        $this->loadModel('Line');        
        $this->loadModel('UserArea');
        $this->loadModel('Shift');

        $appBosch = new stdClass();
        $appBosch->lines = array();
        $appBosch->teams = array();
        $user = $this->Auth->user();
        $areas = $this->UserArea->getByUser($user['id']);
        foreach( $areas as $area )
        {
            $areaId = $area['area_id'];
            $teams  = $this->Team->getEnabledByArea($areaId);
            foreach( $teams as $team )
            {
                array_push($appBosch->teams, $team);
            }
            $areaData = array(
                'virtual' =>  $this->Line->getEnabledByAreaAndType($areaId, Line::TYPE_VIRTUAL),
                'real' => $this->Line->getEnabledByAreaAndType($areaId, Line::TYPE_REAL)
            );
            $appBosch->lines[$areaId] = $areaData;
        }
        $appBosch->shifts = $this->Shift->getEnabledOrderByCode();
        $appBosch->areas = $areas;
        $appBosch->realLines = array();
        $appBosch->virtualLines = array();
        $appBosch->msgDestroy = __('¿Estás seguro que deseas eliminar este equipo?');
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Equipos'));
    }

    public function magic($areaId) {
        $this->request->allowMethod('get');
        $this->loadModel('Team');
        $this->loadModel('Line');
        $realLines = $this->Line->getEnabledByAreaAndType($areaId, Line::TYPE_REAL);
        $virtualLines = $this->Line->getEnabledByAreaAndType($areaId, Line::TYPE_VIRTUAL);
        $appBosch = new stdClass();
        $appBosch->areaId = $areaId;
        $appBosch->teams = $this->Team->getEnabledByArea($areaId);
        $appBosch->realLines = $realLines;
        $appBosch->virtualLines = $virtualLines;
        $appBosch->msgDestroy = __('¿Estás seguro que deseas eliminar este grupo?');
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Equipos'));
    }

    public function saveName($id) {
        $this->loadModel('ShiftModel');
        $this->loadModel('Team');
        $name = $this->request->query['name'];
        $shift = $this->request->query['shift'];
        $m = $this->_model;
        $this->$m->id = $id;
        $record = $this->$m->read();
        if ($shift == '') {
            $shift = null;
        }
        $this->ShiftModel->updateShiftByTeam($id);
        $record[$m]['shift_id'] = $shift;
        $record[$m]['name'] = $name;
        $this->$m->save($record[$m]);
        $result = $this->Team->getEnabledByArea(null, $id);
        $this->set(array('record' => $result[0], '_serialize' => 'record'));
        $this->viewClass = 'Json';
    }

    public function getBySeed($areaId) {
        $seed = $this->request->query['seed'];
        $areaIds = array();
        if( $areaId != 0 )
        {
            array_push( $areaIds, $areaId );
        }
        $areas = $this->request->query('areas');
        if( $areas )
        {
            foreach( $areas as $value )
            {
                array_push( $areaIds, $value );
            }
        }
        $m = $this->_model;
        $records = array();
        foreach ($areaIds as $nAreaId) 
        {
            $tmpRecords = $this->$m->getBySeed($nAreaId, $seed);
            foreach($tmpRecords as $record) 
            {
                array_push($records, $record);
            }
        }        
        $this->set(array('records' => $records, '_serialize' => 'records'));
        $this->viewClass = 'Json';
    }

    /**
     * AJAX
     * Obtenemos los detalles de un equipo
     * @param string $teamId UUID
     */
    public function resumen($teamId) {
        $this->request->allowMethod('get');
        $team = false;
        if ($this->request->is('ajax') === true) {
            $this->loadModel('TeamLine');
            $this->loadModel('TeamUser');
            $this->Team->id = $teamId;
            $team = $this->Team->read();
            $team['lines'] = $this->TeamLine->getEnableByTeam($teamId);
            $team['users'] = $this->TeamUser->getTeam($teamId);
        }
        $this->set(array('team' => $team, '_serialize' => 'team'));
        $this->viewClass = 'Json';
    }

    public function updTeamConf()
    {
        $this->request->allowMethod('get');
        $team = false;
        if ($this->request->is('ajax') === true)
        {
            $this->loadModel('TeamLine');
            $this->loadModel('Line');
            $teamId = $this->request->query['teamId'];
            $resTeam = $this->Team->findById( $teamId );
            $team    = $resTeam['Team'];
            $lineId  = $this->request->query['lineId'];
            $resLine = $this->Line->findById($lineId);
            $line = $resLine['Line'];
            $data   = array(
                'line_id' => $lineId,
                'area_id' => $line['area_id']
            );
            $this->Team->update($teamId, $data );
            $fields = array(
                'status' => TeamLine::STATUS_DISABLED,
            );
            $conditions = array(
                'team_id'=>$team['id']
            );
            $this->TeamLine->updateAll($fields, $conditions);
            $resTeam = $this->Team->findById($teamId);
            $team   = $resTeam['Team'];
        }
        $this->set(array('team' => $team, '_serialize' => 'team'));
        $this->viewClass = 'Json';
    }
    
    public function cleanTeam()
    {
        $this->request->onlyAllow('get');
        $res = false;
        if ($this->request->is('ajax') === true && isset($this->request->query['teamId']) ) 
        {
            $teamId = $this->request->query['teamId'];
            $this->loadModel('TeamLine');            
            $this->loadModel('TeamUser');            
            $fields = array(
                'status' => TeamLine::STATUS_DISABLED,
            );
            $conditions = array(
                'team_id'=>$teamId
            );
            $resTL = $this->TeamLine->updateAll($fields, $conditions);
            $fields = array(
                'status' => TeamUser::STATUS_DISABLED,
            );
            $resTU = $this->TeamUser->updateAll($fields, $conditions);
            
            $res = array( 'deleted'=>true );
        }
        $this->set(array('res' => $res, '_serialize' => 'res'));
        $this->viewClass = 'Json';
    }

    protected function getRecords($param)
    {
        $m = $this->_model;
        $records = $this->$m->getEnabledByLine($param);
        return $records;
    }

    protected function c($model)
    {
        $m = $this->_model;
        return array(
            'id' => $model->id,
            'area_id' => $model->area_id,
            'name' => mb_strtoupper($model->name),
            'status' => $m::STATUS_ENABLED,
            'line_id' => $model->line_id,
            'line' => $model->line,
            'colour_line' => $model->colour_line,
            'colour_real' => $model->colour_real,
        );
    }

    protected function u($model)
    {
        return array(
            'line_id' => $model->line_id,
            'shift_id' => $model->shift_id,
            'start_date' => $model->start_date,
            'end_date' => $model->end_date,
        );
    }

}
